let CryptoJS = require('crypto-js');
let { RSAKeyPair, setMaxDigits, encryptedString } = require('./crypto-utils');

function a(a) {
  var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 
      c = "";
  for (d = 0; a > d; d += 1) e = Math.random() * b.length, e = Math.floor(e), c += b.charAt(e);
  return c
}

function b(a, b) {
  var c = CryptoJS.enc.Utf8.parse(b),
      d = CryptoJS.enc.Utf8.parse("0102030405060708"),
      e = CryptoJS.enc.Utf8.parse(a),
      f = CryptoJS.AES.encrypt(e, c, {
          iv: d,
          mode: CryptoJS.mode.CBC
      });
  return f.toString()
}

function c(a, b, c) {
  var d, e;
  return setMaxDigits(131), d = new RSAKeyPair(b, "", c), e = encryptedString(d, a)
}

function d(d, e, f, g) {
  var h = {},
      i = a(16);
  return h.encText = b(d, g), h.encText = b(h.encText, i), h.encSecKey = c(i, e, f), h
}

module.exports = {
  d
};

